<?php
/*********************************************************************************
 * Copyright (C) 2017 CDEDUASK ChuangXinGongChang
 * All Rights Reserved.
 * 本软件为华育国际创新工场开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、复制、修改或发布本软件.
 *********************************************************************************/
/**
 * xxxxx
 * User: 蒋维
 * Date: 2017/7/26
 * Time: 13:43
 */


namespace app\home\controller;
use think\Controller;
use think\Cookie;
use think\Db;
use think\Request;

class Chapter extends Controller
{
    public  function chaptershow(){
        if(Cookie::has("user_id")){//判断登录状态
            //根据传过来的course_id（就是get['id']）   查询出课程的章节和课节
            $charge=Db::table("course")->field("charge")->where("id={$_GET["id"]}")->find();
           $buying_courses=Db::table("buying_courses")->where("user_id=".Cookie::has("user_id")." and course_id=".$_GET["id"])->find();
            //判断是否购买
            if($charge["charge"]>0&&empty($buying_courses)){
                            $this->redirect(url("buying_courses")."?course_id=".$_GET["id"]);
            }else{
                //查询章节
                $chapter= Db::table("course_chapter")->where("course_id=".$_GET["id"])->order("course_id asc")->select();
                $course=Db::table("course")->where("id=".$_GET["id"])->find();
//        pre($chapter);
//        pre($course);
                $counts=0;
                for($ii=0;$ii<count($chapter);$ii++){
                    //查询每个课程的课节
                    $chapter[$ii]["file"]=Db::table("course_file")->where("course_chapter_id=".$chapter[$ii]["id"])->select();
                    $counts+=count($chapter[$ii]["file"]);
                }
                        //判断是否 存在课节文件id 有就赋值 否就默认为第一章节
                if(isset($_GET["videofileid"])){
                    $videofile=Db::table("course_file")->field("url")->where("id={$_GET["videofileid"]}")->find()["url"];
                }else{
                    if(empty($chapter[0]["file"])){
                        $videofile= "";
                    }else{
                        $videofile=   $chapter[0]["file"][0]["url"];
                    }
                }
                $foreach["count"]=count($chapter);
                $foreach["counts"]=$counts;
                $foreach["difficulty"]=$course["difficulty"];
//        pre($chapter);
                return view("chaptershow",["foreach"=>$foreach,"chapter"=>$chapter,"course"=>$course,"videofile"=>$videofile]);
            }
        }else{
            //没有登录状态就跳转到登录页面
            $this->redirect("Index/login");
        }
}
public function buying_courses(){
  //    查询课程费用
        $money=DB::table("course")->field("id,charge,record_name")->where("id=".$_GET["course_id"])->find();
        return view("buying_courses",["course"=>$money]);
}
public function moeny(){
//    pre($_POST);
//    该方法用于判断用户余额是否充足
   $course_charge= Db::table("course")->field("charge")->where("id=".$_POST["course_id"])->find();
   $user_charge= Db::table("user")->field("balance")->where("id=".Cookie::get("user_id"))->find();
        if($course_charge["charge"]<=$user_charge["balance"]){
            return true;
        }else{
            return false;
        }
}
public  function purchase(){ //    购买课程
//    pre($_POST);
    $user=Db::table("user")->field("balance")->where("id=".Cookie::get("user_id")." and password='".md5($_POST['password'])."'")->find();
    $repeat=Db::table("buying_courses")->field("id")->where("user_id=".Cookie::get("user_id")." and course_id=".$_POST["course_id"])->find();
    if(empty($repeat)){//判断是否否买过了
        if(empty($user)){//如果用户信息为空   返回密码错误
            $state["content"]="password_no";
            echo json_encode($state);
        }else{
            $course=Db::table("course")->field("charge")->where("id=".$_POST["course_id"])->find();//查询课程付费
            $balance=$user["balance"]-$course["charge"];
            Db::startTrans();
            if(Db::table("user")->where("id=".Cookie::get("user_id")," and password='".md5($_POST["password"])."'")->update(["balance"=>$balance])){
                //如果用户余额减少
                if(Db::table("buying_courses")->insert(["user_id"=>Cookie::get("user_id"),"course_id"=>$_POST["course_id"]])){
//                    如果添加购买信息成功
                    if(Db::table("recharge_record")->insert(
                        [
                            "user_id"=>Cookie::get("user_id"),
                            "time"=>date("Y年-m月-d日 H:i:s"),
                            "state"=>"支出",
                            "increase"=>$course["charge"]
                        ])){
//                        如果添加购买记录成功
                        $state["content"]="balance_true";//返回添加成功
                        Db::commit();
                        echo json_encode($state);
                    }else{
                        Db::commit();
                        $state["content"]="balance_false";//返回添加失败
                        echo json_encode($state);
                    }

                }else{
                    Db::commit();
                    $state["content"]="balance_false";//返回添加失败
                    echo json_encode($state);
                }
            }else{
                Db::commit();
                $state["content"]="balance_false";//返回添加失败
                echo json_encode($state);
            }
        }
    }else{
        $state["content"]="repeat";//返回购买过了
        echo json_encode($state);
    }
}


public  function course_video(){//这个方法已淘汰
//   pre($_GET);
    $type=Db::table("course")->field("category")->where("id",$_GET["cid"])->find();
    $course=Db::table("course")->where("category",$type["category"])->limit(3)->select();
    $file=Db::table("course_file")->field("url ,type")->where("course_chapter_id",$_GET["id"])->find();
//   pre($file);
   return  view("course_video",["course"=>$course,"files"=>$file]);
}

public function video(){//该方法已淘汰
    if(Request::instance()->isAjax()){
        $file=Db::table("course_file")->field("url ,type")->where("id",$_POST["fileid"])->find();
        return  $file;
    }else{
        return false;
    }
}
}